import {Route,Redirect,Switch} from 'react-router-dom'
import {mainRoutes} from './router/index'
import {connect} from 'react-redux'
import Main from './components/Main/index.jsx'
// let isLogin = false;  //模拟用户登录状态
function App(props) {
  console.log('App组件渲染');
  return (
    <div className="App">
      {/* 在此处做第一层路由控制 */}
      {/* <Route path='/login' component={Login} />
      <Route path='/404' component={NotFound} /> */}
      <Switch>
        <Route path="/admin" render={() => {
          console.log('路由拦截', props.userReducer.isLogin);
          if (props.userReducer.isLogin) {
            return <Main/>
          } else {
            return <Redirect to="/login" />
          }
        }} />
        {
          mainRoutes.map((item, index) => {
            return <Route
              path={item.path}
              component={item.component}
              key={index}
            />
          })
        }
        <Redirect from="/" exact to="/admin" />
        <Redirect from="*" to="/404" />
      </Switch>
    </div>
  );
}
const mapState = (state)=>{
  return state  //将状态机中的用户状态信息，提取并注入到App的props中
}
export default connect(mapState)(App);
